<?php
/**
 * Created by PhpStorm.
 * User: Elton
 * Date: 2021/5/31
 * Time: 15:10
 */

/**
 * 选择排序：挑选最小的值放到最开始的位置
 * @param array $arr
 * @return array
 */
function selectionSort(array $arr)
{
    $len = count($arr);

    $minIndex = 0;
    for ($i = 0; $i < $len - 1; $i++) {
        for ($j = $i + 1; $j < $len - 1; $j++) {
            if( $arr[$j] < $arr[$minIndex]){
                $minIndex = $j;
            }
        }
        $tmp = $arr[$i];
        $arr[$i] = $arr[$minIndex];
        $arr[$minIndex] = $tmp;
    }
    return $arr;

}

$a = [10, 30, 20, 50, 9, 19, 8];

print_r(selectionSort($a));
